service.factory('orderFactory',['$q','appFactory',function($q,appFactory){

    var Order = AV.Object.extend('Order');

    var Mission = AV.Object.extend('Mission');

    //外卖订单的类型;
    var OUT_ORDER_TYPE = [6,8];

    //订单的字段信息;
    var ORDER_FIELDS = ['remark','payway','uid','address','messioned','orderid','total','paytime','type','finalAmount','description','areaId','shopId','status','goods','location'];

    //列表需要的状态码
    var ORDER_STATUS = [2500,3000,4000];

    var get0Time = function(){
        var today = new Date();
        today.setHours(0);
        today.setMinutes(0);
        today.setSeconds(0);
        today.setMilliseconds(0);
        return today.getTime()/1000;
    }

    var getNext0Time = function(){
        return get0Time() + 60 * 60 * 24;
    }

    return {
        PAGE_SIZE:5,
        getOutOrderList:function(options){

            var todayZeroTime = get0Time();
            var tomZeroTime = todayZeroTime + 60 * 60 * 24;

            var myOrder = new AV.Query(Order);
            myOrder.select(ORDER_FIELDS);
            myOrder.limit(this.PAGE_SIZE);
            myOrder.containedIn('status',ORDER_STATUS);
            myOrder.containedIn('type',OUT_ORDER_TYPE);
            myOrder.containedIn('areaId',options.areaId);
            myOrder.notEqualTo('messioned',true);
            myOrder.addDescending('status');
            myOrder.addAscending('paytime');

            var normalOrder = new AV.Query(Order);
            normalOrder.select(ORDER_FIELDS);
            normalOrder.limit(this.PAGE_SIZE);
            normalOrder.containedIn('status',ORDER_STATUS);
            normalOrder.containedIn('type',OUT_ORDER_TYPE);
            normalOrder.notEqualTo('messioned',true);
            normalOrder.addDescending('status');
            normalOrder.addAscending('paytime');

            var monthOrder = new AV.Query(Order);
            monthOrder.select(ORDER_FIELDS);
            monthOrder.limit(this.PAGE_SIZE);
            monthOrder.containedIn('status',ORDER_STATUS);
            monthOrder.containedIn('type',[7]);
            monthOrder.lessThan('bookdate',''+tomZeroTime);//小于第二天
            monthOrder.greaterThan('bookdate',''+todayZeroTime);//大于凌晨
            monthOrder.notEqualTo('messioned',true);
            monthOrder.addDescending('status');
            monthOrder.addAscending('paytime');

            var query = AV.Query.or(myOrder,normalOrder, monthOrder);
            return query.find();
        },

        rushMession:function(o){
            var q = $q.defer();
            async.waterfall([
                function(cb){
                    //查询订单状态
                    var query = new AV.Query(Order);
                    query.get(o.id).then(function(res){
                        if(res.get('messioned')===true){
                            cb({error:'手慢拉!~刷新一下再试试!'})
                        }else{
                            //更新订单状态
                            //o.set('status',RUSH_ORDER_STATUS);
                            o.set('messioned',true);
                            o.set('messionedUser',''+appFactory.getUserInfo().uid);
                            o.save().then(function(res){
                                cb(null,res);
                            }).catch(function(err){
                                cb(err);
                            });
                        }
                    }).catch(function(err){
                        cb({error:'手慢拉!~刷新一下再试试!'})
                    });
                },
                function(arg0,cb){
                    var m = new Mission();
                    var detail = o.attributes;
                    detail['createdAt'] = o.getCreatedAt();
                    m.save({
                        orderid: o.get('orderid'),
                        uid: ''+appFactory.getUserInfo().uid,
                        status: 1,
                        detail: detail,
                        rushTime: new Date()
                    }).then(function(res){
                        cb(null,res);
                    }).catch(function(err){
                        //重置订单的状态
                        o.remove('messioned');
                        o.save().then(function(res){
                            cb(null,res);
                        }).catch(function(err){
                            cb(err);
                        });
                    });
                }
            ],function(err,res){
                if(err){
                    q.reject({error:'手慢拉!~刷新一下再试试!'});
                }else{
                    q.resolve(res);
                }

            });
            return q.promise;

        }


    }
}]);